package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_Xwfarmfood;
import com.xwdeshj.entity.XwFarmFoodinfo;
import com.xwdeshj.entity.XwFarmRoominfo;
import com.xwdeshj.repository.XwFarmFoodinfoRepository;
import com.xwdeshj.service.XwFarmFoodinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by BKWJA on 2019/1/6.
 * 农家乐——农家美食信息 实现类
 */
@Slf4j
@Service("xwFarmFoodinfoService")
public class XwFarmFoodinfoServiceImpl implements XwFarmFoodinfoService {

    @Autowired
    private XwFarmFoodinfoRepository xwFarmFoodinfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_Xwfarmfood dal_xwfarmfood = new Dal_Xwfarmfood();

    /**
     *  获取农家乐店铺 美食信息
     */
    @Override
    public JSONObject getfarmfoodlist(Integer pageindex, Integer pagesize, String starttime, String endtime,
                                      String sortfild, String sortway, String searchval, String searchkey,
                                      String farm_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwfarmfood.getfarmfoodlist(pageindex,pagesize,starttime,endtime,sortfild,sortway,
                searchval,searchkey,farm_encryptId,jdbcTemplate);

        System.out.println("getfarmfoodlist:"+result.toString());
        return result;
    }


    /**
     * 新建 修改农家 美食信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject updatefarmfood(Integer food_id, String foodType, String foodremarks, String foodprice,
                                     String foodnum, String farm_encryptId, String business_encryptId,
                                     HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){

            if(food_id == null || food_id == 0){
                //新建
                XwFarmFoodinfo xwFarmFoodinfo = new XwFarmFoodinfo();
                xwFarmFoodinfo.setFarmEncryptid(farm_encryptId);
                xwFarmFoodinfo.setBusinessEncryptid(business_encryptId);
                xwFarmFoodinfo.setFarmFoodType(foodType);
                xwFarmFoodinfo.setFarmFoodDesc(foodremarks);
                xwFarmFoodinfo.setFarmFoodPrice(Double.parseDouble(foodprice));
                xwFarmFoodinfo.setFarmFoodNumber(Integer.parseInt(foodnum));
                xwFarmFoodinfo.setFarmFoodCreatetime(new Date());
                xwFarmFoodinfo.setFarmFoodUpdatetime(new Date());
                try {
                    xwFarmFoodinfoRepository.save(xwFarmFoodinfo);
                    result.put("status", 0);
                    result.put("msg", "ok");

                }catch (Exception e){
                    e.printStackTrace();
                    result.put("status",40002);
                    result.put("msg",e);
                }

            }else {
                //修改
                XwFarmFoodinfo xwFarmFoodinfo = new XwFarmFoodinfo();
                xwFarmFoodinfo.setFarmFoodId(food_id);
                boolean tmep = xwFarmFoodinfoRepository.exists(Example.of(xwFarmFoodinfo));
                if(tmep){
                    XwFarmFoodinfo farmFoodinfo = xwFarmFoodinfoRepository.findByFarmFoodId(food_id);
                    farmFoodinfo.setFarmFoodType(foodType);
                    farmFoodinfo.setFarmFoodDesc(foodremarks);
                    farmFoodinfo.setFarmFoodPrice(Double.parseDouble(foodprice));
                    farmFoodinfo.setFarmFoodNumber(Integer.parseInt(foodnum));
                    farmFoodinfo.setFarmFoodUpdatetime(new Date());

                    try {
                        xwFarmFoodinfoRepository.save(farmFoodinfo);
                        result.put("status", 0);
                        result.put("msg", "ok");

                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }
                }
            }

        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("updatefarmfood:"+result.toString());
        return result;
    }

    /**
     *   根据美食id，删除对应的信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delfarmfood(String food_id, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!food_id.equals("")){
                if(food_id.indexOf(",") == -1){
                    //单个删除
                    int flag = xwFarmFoodinfoRepository.deleteByFarmFoodId(Integer.parseInt(food_id));
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",500);
                        result.put("msg","删除失败！");
                    }
                }else {
                    //批量删除
                    int flag = 0;
                    String[] tids = food_id.split(",");
                    for (int i = 0; i < tids.length; i++) {
                        flag = xwFarmFoodinfoRepository.deleteByFarmFoodId(Integer.parseInt(tids[i]));
                    }
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",500);
                        result.put("msg","删除失败！");
                    }
                }
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("delfarmfood:"+result.toString());
        return result;
    }




    public Dal_Xwfarmfood getDal_xwfarmfood() {
        return dal_xwfarmfood;
    }

    public void setDal_xwfarmfood(Dal_Xwfarmfood dal_xwfarmfood) {
        this.dal_xwfarmfood = dal_xwfarmfood;
    }
}
